package com.wandian.centerserver;

import com.wandian.texas.BaseCommandObject;
import com.wandian.texas.CommandObject;
import com.wandian.texas.DataPacket;
import com.wandian.texas.GameData;
import com.wandian.texas.SessionMgr;
import com.wandian.texas.SessionObject;
import com.wandian.texas.ThreadPool;
import com.wandian.texas.ThreadRun;
import com.wandian.texas.protocol.UserProtocol;
import com.wandian.util.Log;

/**
 * @author long
 * @date 2012-12-7 下午3:04:06
 * @qq 108232706
 * @description CenterSessionMgr中心服务控制器实现
 */
public class CenterSessionMgr extends SessionMgr {

	private static CenterSessionMgr instance = null;
	/**
	 * 游戏服务器登录验证
	 */
	public static final short CMD_CENTER_LOGIN = 15000;
	/**
	 * 请求中心服务器操作
	 */
	public static final short CMD_CENTER_SERVER = 15001;
	/**
	 * 是否允许出积宝
	 */
	public static final short CMD_ALLOWCORNUCOPIA = 15002;
	/**
	 * 游戏端通知,游戏正在开始，已使用给定的积宝指标
	 */
	public static final short CMD_USINGDCORNUCOPIA = 15003;
	/**
	 * 游戏端结束游戏,通知服务端积宝产生了
	 */
	public static final short CMD_SENDCORNUCOPIA = 15004;
	/**
	 * 中心服务器广播积宝中奖事件
	 */
	public static final short CMD_BROADCASTSENDCORNUCOPIA = 15005;
	/**
	 * 中心服务器广播积宝在制定延时没有使用，失效
	 */
	public static final short CMD_BROADCASTTIMEOUTCORNUCOPIA = 15006;
	/**
	 * 中心服务器广播30秒更新一次积宝
	 */
	public static final short CMD_BROADCASTUPDATECORNUCOPIAPER30S = 15007;
	/**
	 * 中心服务器定时器任务
	 */
	public static final short CMD_CENTER_SERVER_TIMERS = 15008;
	/**
	 * 游戏结束盈利，发送给中心服，使其能抽成至积宝
	 */
	public static final short CMD_SENDSYSTEMTOTALWIN = 15009;

	public CenterSessionMgr(Single single) throws Throwable {
		super(new CenterIoHandler());
		if (single == null) {
			throw new IllegalArgumentException("Can\'t create instance , Single is Null!");
		}
	}

	public synchronized static CenterSessionMgr getInstance() {
		if (instance == null) {
			try {
				syncInit();
			} catch (Throwable e) {
				e.printStackTrace();
			}
		}
		return instance;
	}

	private static synchronized void syncInit() throws Throwable {
		if (instance == null) {
			instance = new CenterSessionMgr(new Single());
		}
	}

	/* 检查登陆 */
	@Override
	public void OnCheck(SessionObject sessionObj, BaseCommandObject command) {
		// 玩家状态为1,在等待外面调用检查，
		try {
			if (command == null) {
				return;
			}
			if (command.getCmd() != CenterSessionMgr.CMD_CENTER_LOGIN)// 第一个包一定是关于1000登陆
			{
				sessionObj.setStatus(GameData.DISCONNECT);
				sessionObj.cmdStatus[5] = 0;
				// 命令如果不push到线程池，必须手动回收
				ThreadPool.getInstance().freeUsedCommand(command);
				return;
			}
			sessionObj.cmdStatus[1] = 1;
			// byte[] bytes = dataPacket == null ? null : dataPacket.bytes;
			ThreadPool.getInstance().Push(command);
			// ThreadPool.getInstance().Push(sessionObj, dataPacket.cmdId,
			// bytes);
		} catch (Exception e) {
			Log.info("OnCheck Push command failure!", e);
		}

	}

	@Override
	public void OnCheckSuss(SessionObject sessionObj, BaseCommandObject command) {
		Log.info("OnCheckSuss");
		if (command != null) {
			// 命令如果不push到线程池，必须手动回收
			ThreadPool.getInstance().freeUsedCommand(command);
		}
	}

	@Override
	public void OnCommand(SessionObject sessionObj, BaseCommandObject command) {
		try {
			if (command == null) {
				return;
			}
			ThreadPool.getInstance().Push(command);
		} catch (Exception e) {
			Log.info("OnCommand Push command failure!", e);
		}
	}

	@Override
	public void OnConnect(SessionObject sessionObj, BaseCommandObject command) {
		// 停止外面，该玩家已经验证通过，等待置为Ready状态
		sessionObj.setStatus(GameData.READYGAME);
		if (command != null) {
			// 命令如果不push到线程池，必须手动回收
			ThreadPool.getInstance().freeUsedCommand(command);
		}
	}

	@Override
	public void OnDisconncet(SessionObject sessionObj, BaseCommandObject command) {
		if (command != null) {
			// 命令如果不push到线程池，必须手动回收
			ThreadPool.getInstance().freeUsedCommand(command);
		}

	}

	@Override
	public void OnWaitCheck(SessionObject sessionObj, BaseCommandObject command) {
		sessionObj.setStatus(GameData.WAITINGCHECKSUCC);
		if (command != null) {
			// 命令如果不push到线程池，必须手动回收
			ThreadPool.getInstance().freeUsedCommand(command);
		}

	}

}

class Single {

}
